var express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
const movieSchema = new mongoose.Schema({
 title:String
});
mongoose.model('movieModel', movieSchema, 'm');


const actorSchema = new mongoose.Schema({
  name: String,
  //关联到电影
  moviesId: {
    type: mongoose.SchemaTypes.ObjectId,
    ref: "movieModel"
  }
});
mongoose.model('actorModel', actorSchema, 'actors');


router.get('/', function(req, res, next) {
  res.send('返回电影数据');
});
router.put('/',async function (req, res, next) {
  //将关联的电影id保存到数据库。
  let arr = req.body.moviesId.split(',');
  console.log(arr);
  //let result = await mongoose.model("actorModel").updateOne({ _id: req.body.id }, { moviesId: arr[0] });
  let data = await mongoose.model('actorModel').find({ _id: req.body.id }).populate('moviesId');
  console.log(data);
  res.send({
    info:req.body,
    data:data
  });
});

module.exports = router;
